#include "network.h"
#include <iostream>

Network &Network::getInstance()
{
    static Network instance;
    return instance;
}

Network::Network() {
    networkThread = nullptr;
}

void Network::start()
{
    std::cerr << "net work start" << std::endl;
    if (networkThread) {
        networkThread->join();
    }
    // 创建并启动新的线程
    networkThread = std::make_shared<std::thread>(std::thread(&hv::TcpClient::start, this, true));
}

void Network::join()
{
    hv::TcpClient::stop();
    std::cerr << "stop client" << std::endl;
    if (networkThread && networkThread->joinable()) {
        networkThread->join();
        networkThread = nullptr;
    }
}

Network::~Network() {
    if (networkThread && networkThread->joinable()) {
        networkThread->join();
    }
}
